<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact_model extends CI_Model {

    private $_name = 'ci_user';

    var $validate = array(
        'contact_add' => array(
                    array('field'   => 'name'         ,'rules'   => 'required|max_length[50]|xss_clean',),
                    array('field'   => 'nation'       ,'rules'   => 'required|max_length[50]|xss_clean',),
                    array('field'   => 'phone'        ,'rules'   => 'required|max_length[50]|xss_clean',),
                    array('field'   => 'email'        ,'rules'   => 'required|max_length[50]|valid_email|xss_clean',),
                    array('field'   => 'company_name' ,'rules'   => 'required|max_length[50]|xss_clean',),
                    array('field'   => 'content'      ,'rules'   => 'required|max_length[9999]|xss_clean',)
                ),
        );

    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->library(array('form_validation','MY_Validation','MY_FormParam'));
    }

    public function getContactList($params = array(), $isUsePager = false, $per_page = 0, $offset = 0, $order = 'contact.lastup_datetime DESC', $table = 'contact'){
        if ($isUsePager) {
            $this->db->limit($per_page, $offset);
        }

        $where = $this->getWhere($params);
        $this->db->where($where , NULL , FALSE);
        if(!is_null($order)){
            $this->db->order_by($order);
        }
        $query = $this->db->get($table);

        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $result[] = $rows;
            }
            return $result;
        }
    }

    public function getContactById($contact_id){
        $query = $this->db->get_where('contact',array('contact_id' => $contact_id),1);
        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $result = $rows;
            }
            return $result;
        }
        return false;
    }
    
    private function getWhere($params){
        $where = ' disable = 0 ';
        if(!empty($params)){
            foreach($params as $key => $value){
                if(is_numeric($value)) {
                    settype($value, 'string');
                }
                if($value == ''){
                    continue;
                }
                switch ($key) {
                    case 'key':
                        $where .= " AND `key` = '$value'";
                        break;
                    case 'status':
                        $where .= " AND `status` = $value";
                        break;
                    case 'expire':
                        $where .= " AND ('$value' - INTERVAL 1 DAY) <= regist_datetime";
                        break;
                    default:
                        break;
                }
            }
        }
        return $where;
    }
    
    function exec_validation($valid_type) {
        $array    = $this->my_formparam->getFormParamList();
        $arrValidate = $this->validate[$valid_type];

        foreach($arrValidate as &$value){
            if($value['field'] == $array[$value['field']]['keyname']) {
                $value['label'] = $array[$value['field']]['disp_name'];
            }
        }

        $this->my_validation->set_rules($arrValidate);

        $status = $this->my_validation->run();

        $data['status'] = $status;
        $data['data'] = &$this->my_validation->_field_data;

        return $data;
    }
}
?>
